<?php

namespace App\Models;

use App\Models\Contract\SerializeDatetime;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AdrewardDetail extends Model
{
    use HasFactory, SerializeDatetime;

    protected $fillable = [
        'hashid', 'adreward_id', 'amount', 'adtype_id', 'adtype_name', 'admodel_id', 'admodel_value',
        'reward_rate', 'exchange_rate', 'real_money', 'displayed_at', 'user_id',
        'user_money', 'ecpm', 'appcode', 'collect_status', 'user_ip'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function admodel(){

        return $this->belongsTo(Admodel::class,'admodel_id','id');
    }
    /**
     * 广告奖励唯一标识.
     *
     * @param array $attributes
     * @return string
     */
    public static function hashid(array $attributes): string
    {
        /** @var Carbon $time */
        $displayedAt = $attributes['displayed_at'];
        if (is_string($displayedAt)) {
            $displayedAt = Carbon::parse($displayedAt);
        }
        $time = $displayedAt->format('YmdHis');
        $identical = sprintf(
            "%s_%s_%s_%s",
            $attributes['user_id'], $attributes['adtype_id'], $attributes['admodel_id'], $time
        );

        return md5($identical);
    }

}
